<?php

namespace App\Admin\Controllers;

use App\Models\Tool\Ad;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class AdController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = 'Ad';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Ad());

        $grid->column('id', __('ID'));
        $grid->column('title', __('名称'));
        $grid->column('ad_id', __('广告ID'));
        $grid->column('points', __('奖励积分'));
        $grid->column('view_num', __('浏览量'));
        $grid->column('remark', __('备注'));
        $states = [
            'on'  => ['value' => 1, 'text' => '正常', 'color' => 'success'],
            'off' => ['value' => 0, 'text' => '禁用', 'color' => 'danger'],
        ];
        $grid->column('state', __('状态'))->switch($states);
        $grid->column('create_time', __('创建时间'));
        $grid->column('update_time', __('更新时间'));

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Ad::findOrFail($id));

        $show->field('id', __('ID'));
        $show->field('title', __('名称'));
        $show->field('ad_id', __('广告ID'));
        $show->field('points', __('奖励积分'));
        $show->field('view_num', __('浏览量'));
        $show->field('remark', __('备注'));
        $show->field('state', __('状态'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Ad());

        $form->text('title', __('名称'));
        $form->text('ad_id', __('广告ID'));
        $form->number('points', __('奖励积分数'))->default(5);
        $form->number('view_num', __('浏览量'));
        $form->text('remark', __('备注'));

        $form->switch('state', __('状态'))->default(1);

        return $form;
    }
}
